﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for TrynParse
/// </summary>
public class TrynParse
{
    TrynParse()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    public static string parseString(object p_objParseValue)
    {
        try { return p_objParseValue.ToString(); }
        catch (NullReferenceException strnullexp) { return string.Empty; }
        catch (FormatException strfmtexp) { return string.Empty; }
    }
    public static bool parseBool(object p_objParseValue)
    {
        bool blnOutput = false;
        bool.TryParse(parseString(p_objParseValue), out blnOutput);
        return blnOutput;
    }
    public static int parseInt(object p_objParseValue)
  {
        int intOutput = 0;
        int.TryParse(parseString(p_objParseValue), out intOutput);
        return intOutput;
    }
    public static double parseDouble(object p_objParseValue)
    {
        double dblOutput = 0d;
        double.TryParse(parseString(p_objParseValue), out dblOutput);
        return dblOutput;
    }
    public static float parseFloat(object p_objParseValue)
    {
        float fltOutput = 0f;
        float.TryParse(parseString(p_objParseValue), out fltOutput);
        return fltOutput;
    }
    public static decimal parseDecimal(object p_objParseValue)
    {
        decimal decOutput = 0m;
        decimal.TryParse(parseString(p_objParseValue), out decOutput);
        return decOutput;
    }
    public static DateTime parseDatetime(object p_objParseValue)
    {
        DateTime dtOutput = new DateTime();
        DateTime dtMinDate = new DateTime(1900, 1, 1);
        DateTime.TryParse(TrynParse.parseString(p_objParseValue), out dtOutput);
        return dtOutput < dtMinDate ? dtMinDate : dtOutput;
    }
    public static int? parseNullableInt(object p_objParseValue)
    {
        if (parseString(p_objParseValue) == "0")
        {
            return null;
        }
        else
        {
            int intOutput;
            int.TryParse(parseString(p_objParseValue), out intOutput);
            return intOutput;
        }
    }
    public static int? parseNullableIntWithZero(object p_objParseValue)
    {
        if (parseString(p_objParseValue) == "")
        {
            return null;
        }
        else
        {
            int intOutput;
            int.TryParse(parseString(p_objParseValue), out intOutput);
            return intOutput;
        }
    }
    public static decimal? parseNullableDecimal(object p_objParseValue)
    {
        if (parseString(p_objParseValue) == "0" || parseString(p_objParseValue) == "0.0" || parseString(p_objParseValue) == "0.00")
        {
            return null;
        }
        else
        {
            decimal intOutput;
            decimal.TryParse(parseString(p_objParseValue), out intOutput);
            return intOutput;
        }
    }
    public static DateTime? parseNullableDate(object p_objParseValue)
    {
        if (parseString(p_objParseValue) == "")
        {
            return null;
        }
        else
        {
            DateTime intOutput;
            DateTime.TryParse(parseString(p_objParseValue), out intOutput);
            return intOutput;
        }
    }

    public static DateTime? parseNullableCompleteDate(object p_objParseValue)
    {
        if (parseString(p_objParseValue) == "")
        {
            return null;
        }
        else
        {
            DateTime intOutput;
            DateTime.TryParse(parseString(p_objParseValue), out intOutput);
            return intOutput.AddDays(1).AddSeconds(-1);
        }
    }

    public static TimeSpan? parseNullableTimeSpan(object p_objParseValue)
    {
        if (parseString(p_objParseValue) == "")
        {
            return null;
        }
        else
        {
            TimeSpan intOutput;
            TimeSpan.TryParse(parseString(p_objParseValue), out intOutput);
            return intOutput;
        }
    }
}
